package com.action.sale;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.PaymentDAO;
import com.dao.SaleOrderDAO;
import com.dao.SaleOrderItemDAO;
import com.dao.impl.PaymentDAOImpl;
import com.dao.impl.SaleOrderDAOImpl;
import com.dao.impl.SaleOrderItemDAOImpl;
import com.model.Customer;
import com.model.ItemTemp;
import com.model.Payment;
import com.model.SaleOrder;
import com.model.SaleOrderItem;
import com.model.SaleOrderNo;

public class AddSaleOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static SaleOrder saleOrder = new SaleOrder();
	private static int id = 1;

	// 结算
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");

		String customerNo = request.getParameter("customerNo");
		String total = request.getParameter("total");
		String state = request.getParameter("state");

		String paymentMethod = request.getParameter("paymentMethod");
		
		SaleOrderDAO sod = new SaleOrderDAOImpl();

		Customer customer = new Customer().getCustomer(customerNo);
		int number = sod.getLastNum() + 1;
		String saleOrderNo = new SaleOrderNo().getSaleOrder("so", number);
		
		// SaleOrder saleOrder = new SaleOrder();
		saleOrder.setId(id);
		saleOrder.setSaleOrderNo(saleOrderNo);
		saleOrder.setCustomer(customer);
		saleOrder.setTotal(Double.parseDouble(total));
		saleOrder.setCreateDateTime(new Date());
		saleOrder.setState(Integer.parseInt(state));
		
		Payment payment = new Payment();
		PaymentDAO pd = new PaymentDAOImpl();
		int paymentNumber = pd.getLastNum() + 1;
		String paymentNo = new SaleOrderNo().getSaleOrder("pay", paymentNumber);
		payment.setPaymentNo(paymentNo);
		payment.setPaymentMethod(Integer.parseInt(paymentMethod));
		payment.setAmount(Double.parseDouble(total));
		payment.setSaleOrder_Id(saleOrder.getId());
		System.out.println("payment"+payment);
		boolean bb = pd.addPayment(payment);
		System.out.println(bb);
		
		boolean b = sod.addOrder(saleOrder);
		id = id + 1;
		String resultJson = null;

		if (b) {
			resultJson = "{\"success\":true,\"msg\":\"销售完成！\"}";
		} else {
			resultJson = "{\"success\":false,\"msg\":\"销售失败!\"}";
		}
		response.getWriter().print(resultJson);
		response.getWriter().flush();
	}

	// 明细
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		List<ItemTemp> itemList = (List<ItemTemp>) session
				.getAttribute("ItemTempList");
		SaleOrderItemDAO soid = new SaleOrderItemDAOImpl();
		for (int i = 0; i < itemList.size(); i++) {
			ItemTemp itemTemp = itemList.get(i);
			SaleOrderItem saleOrderItem = new SaleOrderItem();
			saleOrderItem.setSaleOrder(saleOrder);
			saleOrderItem.setProduct(itemTemp.getProduct());
			saleOrderItem.setQuantity(itemTemp.getQuantity());
			saleOrderItem.setPrice(itemTemp.getSubtotal());
			soid.addSaleOrderItem(saleOrderItem);
		}
		SaleOrderDAOImpl.itemTempList.clear();
		SaleOrderDAOImpl.id = 1;
		saleOrder = new SaleOrder();
		session.setAttribute("ItemTempList", null);
	}
}
